package emap;

class Coord {
	public int X,Y;
	public Coord(){X=0;Y=0;}
	public Coord(int a,int b){X=a;Y=b;}
	public Coord(Coord tocopy){X=tocopy.X;Y=tocopy.Y;}
	public void mlt(double multiplier){X*=multiplier;Y*=multiplier;}
	public void add(Coord toadd){X+=toadd.X;Y+=toadd.Y;}
	public void sbt(Coord tosubtract){X-=tosubtract.X;Y-=tosubtract.Y;}
	public void mvx(int tomove){X+=tomove;}
	public void mvy(int tomove){Y+=tomove;}
	public String toString(){return "("+X+","+Y+")";}
}

class Rect {
	public int X1,Y1,X2,Y2;
	public Rect(){X1=0;Y1=0;X2=0;Y2=0;}
	public Rect(int a,int b){X1=0;Y1=0;X2=a;Y2=b;clean();}
	public Rect(int a,int b,int c,int d){X1=a;Y1=b;X2=c;Y2=d;clean();}
	public Rect(Coord a,Coord b){X1=a.X;Y1=a.Y;X2=b.X;Y2=b.Y;clean();}
	public Rect(Rect a){X1=a.X1;Y1=a.Y1;X2=a.X2;Y2=a.Y2;clean();}
	public void clean(){ //Please clean up after yourself when modifying rects! They don't work otherwise!
		if(X2<X1){int a=X1;X1=X2;X2=a;}
		if(Y2<Y1){int a=Y1;Y1=Y2;Y2=a;}
		}
	public boolean containsCoord(Coord a){if(a.X<X1 || a.X>X2 || a.Y<Y1 || a.Y>Y2) return false;return true;}
	public boolean isIntersecting(Rect a){if(a.X1>X2 || a.X2<X1 || a.Y1>Y2 || a.Y2<Y1) return false;return true;}
	public void moveInside(Rect a){ //Moves this rect into the rect 'a'
		boolean xdone=false,ydone=false;
		if(X2-X1>=a.X2-a.X1){int w=X2-X1;X1=(a.X2+a.X1-w)/2;X2=(a.X2+a.X1-w)/2;xdone=true;}
		if(Y2-Y1>=a.Y2-a.Y1){int w=Y2-Y1;Y1=(a.Y2+a.Y1-w)/2;Y2=(a.Y2+a.Y1-w)/2;ydone=true;}
		if(!xdone && X1<a.X1){X2+=a.X1-X1;X1+=a.X1-X1;}
		if(!xdone && X2>a.X2){X1-=X2-a.X2;X2-=X2-a.X2;}
		if(!ydone && Y1<a.Y1){Y2+=a.Y1-Y1;Y1+=a.Y1-Y1;}
		if(!ydone && Y2>a.Y2){Y1-=Y2-a.Y2;Y2-=Y2-a.Y2;} //Y2K? lol
	}
	public Coord moveInsideOffset(Rect a){ //Makes a Coord representing the amount this rect moved
		Rect b=new Rect(this);
		b.moveInside(a);
		return new Coord(b.X1-X1,b.Y1-Y1);
	}
	public void shoveInside(Rect a){ //Whacks off the parts that don't fit.
		moveInside(a); //Tries to fit it in first
		if(X1<a.X1) X1=a.X1;
		if(X2>a.X2) X2=a.X2;
		if(Y1<a.Y1) Y1=a.Y1;
		if(Y2>a.Y2) Y2=a.Y2;
	}
	public int width(){return X2-X1;}
	public int height(){return Y2-Y1;}
	public void centerOn(Coord a){
		int w=X2-X1;
		int h=Y2-Y1;
		X1=a.X-w/2;
		X2=a.X+w/2;
		Y1=a.Y-h/2;
		Y2=a.Y+h/2;
	}
	public void centerOn(int x,int y){
		int w=X2-X1;
		int h=Y2-Y1;
		X1=x-w/2;
		X2=x+w/2;
		Y1=y-h/2;
		Y2=y+h/2;
	}
	public void scroll(int scrollX,int scrollY){
		X1+=scrollX;
		X2+=scrollX;
		Y1+=scrollY;
		Y2+=scrollY;
	}
	public String toString(){return "[("+X1+","+Y1+"),("+X2+","+Y2+")]";}
}